import 'package:flutter/material.dart';
import 'package:libflutter/language/MineLocalizations.dart';
import 'package:libflutter/main.dart';

class LanguageWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return LanguageState();
  }

  const LanguageWidget();
}

class LanguageState extends State<LanguageWidget> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(MineLocalizations.of(context).title),
      ),
      body: Center(
        child: TextButton(
          child: Text("切换语言"),
          onPressed: () {
            try {
              Locale myLocale = Localizations.localeOf(context);
              print('切换语言 当前>> $myLocale');
              if (myLocale.languageCode == "zh") {
                LocalNotification(const Locale('en', 'US')).dispatch(context);
              } else {
                LocalNotification(const Locale('zh', 'CN')).dispatch(context);
              }
            } catch (e, stack) {
              print('切换语言 Error>> $e $stack');
            }
          },
        ),
      ),
    );
  }
}